﻿using UnityEngine;
using System.Collections;

namespace GUIElements
{
	/// <summary>
	/// Help window.
	/// </summary>
	public class HelpWindow : MonoBehaviour
	{
		private string helpText;
		private string fileName;
		private Rect windowRect;
		private Rect buttonRect;
		private Rect textAreaPos;
		private bool visible;
		private Vector2 activePosition;
		private Vector2 hiddenPosition;
		private Vector2 scrollPosition;

		public GUIStyle windowStyle;
		public GUIStyle buttonStyle;
		public float speed;


		// Use this for initialization
		void Awake ()
		{
			visible = false;
			buttonRect = new Rect (0, 0, buttonStyle.active.background.width, buttonStyle.active.background.height);
			helpText = "";
			fileName = "Assets/Data/help.dat";
			try {
				helpText = System.IO.File.ReadAllText (fileName);
			} catch (System.Exception e) {
				Debug.LogError ("Could not open " + fileName + " " + e.Message);
				helpText = "<ERROR>: Didn't load file.";
			}

			float width = windowStyle.active.background.width;
			float height = windowStyle.active.background.height;

			activePosition = new Vector2 ((float)Screen.width - width + 1, (float)Screen.height * 0.2f);
			hiddenPosition = new Vector2 ((float)Screen.width - (float)buttonStyle.active.background.width, (float)Screen.height * 0.2f);
			windowRect = new Rect (hiddenPosition.x, hiddenPosition.y, width, height);
			textAreaPos = new Rect (buttonRect.width + 2, 0, windowRect.width - buttonRect.width - 2, windowRect.height);
		}
	
		// Update is called once per frame
		void Update ()
		{
	
		}

		void OnGUI ()
		{
			Vector2 currentPosition = new Vector2 (windowRect.x, windowRect.y);
			//Move the window to and from the side
			if (visible && activePosition != currentPosition) {
				currentPosition = Vector2.Lerp (currentPosition, activePosition, speed * Time.deltaTime);
			} else if (!visible && hiddenPosition != currentPosition) {
				currentPosition = Vector2.Lerp (currentPosition, hiddenPosition, speed * Time.deltaTime);
			}
			windowRect.x = currentPosition.x;
			windowRect.y = currentPosition.y;
			GUI.Window (11, windowRect, windowContents, "", windowStyle);
		}

		/// <summary>
		/// Contains the contents of the window.
		/// </summary>
		/// <param name="WindowID">Window I.</param>
		private void windowContents (int WindowID)
		{
			visible = GUI.Toggle (new Rect (0, 0, buttonRect.width, buttonRect.height), visible, "", buttonStyle);
			GUILayout.BeginArea (textAreaPos);
			scrollPosition = GUILayout.BeginScrollView (scrollPosition, GUILayout.Width (textAreaPos.width), GUILayout.Height (textAreaPos.height));
			GUILayout.Label (helpText);
			GUILayout.EndScrollView ();
			GUILayout.EndArea ();
		}

		/// <summary>
		/// Gets or sets a value indicating whether this <see cref="GUIElements.HelpWindow"/> is visible.
		/// </summary>
		/// <value><c>true</c> if visible; otherwise, <c>false</c>.</value>
		public bool Visible {
			get {
				return visible;
			}
			set {
				visible = value;
			}
		}
	}
}
